# Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
#       its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)

# Find package module for COLMAP library.
#
# The following variables are set by this module:
#
#   COLMAP_FOUND: TRUE if COLMAP is found.
#   COLMAP_VERSION: COLMAP version.
#   COLMAP_INCLUDE_DIRS: Include directories for COLMAP.
#   COLMAP_LINK_DIRS: Link directories for COLMAP.
#   COLMAP_LIBRARIES: Libraries required to link COLMAP.
#   COLMAP_CUDA_ENABLED: Whether COLMAP was compiled with CUDA support.
#   COLMAP_CGAL_ENABLED: Whether COLMAP was compiled with CGAL dependencies.

get_filename_component(COLMAP_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_FILE} PATH)
set(COLMAP_INSTALL_PREFIX "${COLMAP_INSTALL_PREFIX}/../..")

set(COLMAP_FOUND FALSE)

# Set hints for finding dependency packages.

set(EIGEN3_INCLUDE_DIR_HINTS @EIGEN3_INCLUDE_DIR_HINTS@)

set(FREEIMAGE_INCLUDE_DIR_HINTS @FREEIMAGE_INCLUDE_DIR_HINTS@)
set(FREEIMAGE_LIBRARY_DIR_HINTS @FREEIMAGE_LIBRARY_DIR_HINTS@)

set(GLEW_INCLUDE_DIR_HINTS @GLEW_INCLUDE_DIR_HINTS@)
set(GLEW_LIBRARY_DIR_HINTS @GLEW_LIBRARY_DIR_HINTS@)

set(GLOG_INCLUDE_DIR_HINTS @GLOG_INCLUDE_DIR_HINTS@)
set(GLOG_LIBRARY_DIR_HINTS @GLOG_LIBRARY_DIR_HINTS@)

# Find dependency packages.

set(TEMP_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH ${COLMAP_INSTALL_PREFIX}/share/colmap/cmake)

if(COLMAP_FIND_QUIETLY)
    find_package(Ceres QUIET)

    find_package(Boost COMPONENTS
                program_options
                filesystem
                regex
                system
                unit_test_framework
                QUIET)

    find_package(Eigen3 QUIET)

    find_package(FreeImage QUIET)

    find_package(Glog QUIET)

    find_package(OpenGL QUIET)
    find_package(Glew QUIET)

    find_package(Qt5 5.4 QUIET COMPONENTS Core OpenGL Widgets)
else()
    find_package(Ceres REQUIRED)

    find_package(Boost COMPONENTS
                program_options
                filesystem
                regex
                system
                unit_test_framework
                REQUIRED)

    find_package(Eigen3 REQUIRED)

    find_package(FreeImage REQUIRED)

    find_package(Glog REQUIRED)

    find_package(OpenGL REQUIRED)
    find_package(Glew REQUIRED)

    find_package(Qt5 5.4 REQUIRED COMPONENTS Core OpenGL Widgets)
endif()

# Set the exported variables.

set(COLMAP_FOUND TRUE)

set(COLMAP_VERSION @COLMAP_VERSION@)

set(COLMAP_OPENMP_ENABLED @OPENMP_ENABLED@)

set(COLMAP_CUDA_ENABLED @CUDA_ENABLED@)
set(COLMAP_CUDA_MIN_VERSION @CUDA_MIN_VERSION@)

set(COLMAP_CGAL_ENABLED @CGAL_ENABLED@)

set(COLMAP_INCLUDE_DIRS
    ${COLMAP_INSTALL_PREFIX}/include/
    ${COLMAP_INSTALL_PREFIX}/include/colmap
    ${COLMAP_INSTALL_PREFIX}/include/colmap/lib
    ${Boost_INCLUDE_DIRS}
    ${EIGEN3_INCLUDE_DIRS}
    ${GLOG_INCLUDE_DIRS}
    ${FREEIMAGE_INCLUDE_DIRS}
    ${CERES_INCLUDE_DIRS}
    ${GLEW_INCLUDE_DIRS}
    ${Qt5Core_INCLUDE_DIRS}
    ${Qt5OpenGL_INCLUDE_DIRS}
    ${Qt5Widgets_INCLUDE_DIRS}
)

set(COLMAP_LINK_DIRS
    ${COLMAP_INSTALL_PREFIX}/lib/colmap
    ${Boost_LIBRARY_DIRS}
)

set(COLMAP_INTERNAL_LIBRARIES
    flann
    graclus
    lsd
    pba
    poisson_recon
    sqlite3
    sift_gpu
    vlfeat
)

set(COLMAP_EXTERNAL_LIBRARIES
    ${CMAKE_DL_LIBS}
    ${GLOG_LIBRARIES}
    ${FREEIMAGE_LIBRARIES}
    ${CERES_LIBRARIES}
    ${OPENGL_gl_LIBRARY}
    ${OPENGL_glu_LIBRARY}
    ${GLEW_LIBRARIES}
    ${Qt5Core_LIBRARIES}
    ${Qt5OpenGL_LIBRARIES}
    ${Qt5Widgets_LIBRARIES}
)

if(UNIX)
    list(APPEND COLMAP_EXTERNAL_LIBRARIES
        ${Boost_FILESYSTEM_LIBRARY}
        ${Boost_PROGRAM_OPTIONS_LIBRARY}
        ${Boost_REGEX_LIBRARY}
        ${Boost_SYSTEM_LIBRARY}
        pthread)
endif()

if(COLMAP_OPENMP_ENABLED)
    find_package(OpenMP QUIET)
    add_definitions("-DOPENMP_ENABLED")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

if(COLMAP_CUDA_ENABLED)
    find_package(CUDA ${COLMAP_CUDA_MIN_VERSION} QUIET)
    list(APPEND COLMAP_EXTERNAL_LIBRARIES ${CUDA_LIBRARIES})
endif()

if(COLMAP_CGAL_ENABLED)
    find_package(CGAL REQUIRED)
    list(APPEND COLMAP_EXTERNAL_LIBRARIES ${CGAL_LIBRARY} ${GMP_LIBRARIES})
endif()

set(COLMAP_LIBRARIES
    colmap
    ${COLMAP_INTERNAL_LIBRARIES}
    ${COLMAP_EXTERNAL_LIBRARIES}
)

# Cleanup of configuration variables.

set(CMAKE_MODULE_PATH ${TEMP_CMAKE_MODULE_PATH})

unset(COLMAP_INSTALL_PREFIX)
unset(EIGEN3_INCLUDE_DIR_HINTS)
unset(FREEIMAGE_INCLUDE_DIR_HINTS)
unset(FREEIMAGE_LIBRARY_DIR_HINTS)
unset(GLEW_INCLUDE_DIR_HINTS)
unset(GLEW_LIBRARY_DIR_HINTS)
unset(GLOG_INCLUDE_DIR_HINTS)
unset(GLOG_LIBRARY_DIR_HINTS)
unset(QT5_CMAKE_CONFIG_DIR_HINTS)
